<?php

class ProductController extends BaseController {
	
	public function __construct() {
		parent::__construct();	
	}
	
	public function restock() {
		$products = new ProductService();
		
		$results = $products->getAllProducts();
		
		$this->view->setDisplay('categories', $results);
		
		$this->view->setTitle(BaliwagConstant::PAGE_TITLE_WITH_DASH . 'Restock');
		
		$this->view->setJavascript(BaliwagConstant::JQUERY_LIB);
		$this->view->setJavascript(BaliwagConstant::JQUERY_UI_JS_LIB);
		$this->view->setJavascript('main');
		$this->view->setJavascript('restock');
		
		$this->view->setCss('main');
		$this->view->setCss('employee');
		$this->view->setCss('ribbon');
		$this->view->setCss('products');
		$this->view->setCss('restock');
		$this->view->setCss(BaliwagConstant::JQUERY_UI_CSS_LIB);
		
		$this->view->template('pages/admin/restock');
	}
	
	public function getProductInfo() {
		$product_id = $this->post('product_id');
		
		$productSvc = new ProductService();
		
		$result = $productSvc->getInfo($product_id);
		
		$data['product_id'] = $result->getProductId();
		$data['product_name'] = $result->getProductName();
		$data['product_current_count'] = $result->getProductQty();
		$data['product_image'] = $result->getImgDirectory();
		
		echo json_encode($data);
	}
	
	public function updateQty() {
		$product_id = $this->post('product_id');
		$product_qty = $this->post('product_qty');
		
		$productSvc = new ProductService();
		
		$result = $productSvc->updateQty($product_id, $product_qty);
	}
	
	public function index() {
		$products = new ProductService();
		
		$results = $products->getAllProductsAdmin();
		
		$this->view->setTitle(BaliwagConstant::PAGE_TITLE_WITH_DASH . 'Product');
		$this->view->setDisplay('categories', $results);
		
		$this->view->setCss('main');
		$this->view->setCss('employee');
		$this->view->setCss('products');
		$this->view->setCss('ribbon');
		$this->view->setCss(BaliwagConstant::JQUERY_UI_CSS_LIB);
		
		$this->view->setJavascript(BaliwagConstant::JQUERY_LIB);
		$this->view->setJavascript(BaliwagConstant::JQUERY_UI_JS_LIB);
		$this->view->setJavascript('main');
		$this->view->setJavascript('product');
		
		$this->view->template('pages/product/product');
	}
	
	public function getCategories() {
		$products = new ProductService();
		
		$results = $products->getProductCategories();
		
		$data = array();
		
		foreach ($results AS $productCategory) {
			$productCategoryData['category_id'] = $productCategory->getCategoryId();
			$productCategoryData['category_name'] = $productCategory->getCategoryName();
			
			$data[] = $productCategoryData;
		}
		
		echo json_encode($data);
	}
	
	public function getProductInformation() {
		$product_id = $this->post('product_id');
		
		$productSvc = new ProductService();
		
		$result = $productSvc->getInformation($product_id);
		
		$product = $result->getProducts();
		
		$data['product_id'] = $product->getProductId();
		$data['product_name'] = $product->getProductName();
		$data['product_price'] = $product->getProductPrice();
		$data['product_current_count'] = $product->getProductQty();
		$data['product_image'] = $product->getImgDirectory();
		$data['product_description'] = $product->getProductDescription();
		$data['product_archive'] = $product->getArchive();
		$data['product_category_id'] = $result->getCategoryId();
		
		echo json_encode($data);
	}
	
	public function updateProduct() {
		
		$productId = $this->post('productId');
		$productName = $this->post('productName');
		$productPrice = $this->post('productPrice');
		$productDescription = $this->post('description');
		$productArchive = $this->post('archive');
		$productCategoryId = $this->post('category');
		$productImg = $this->post('img');
		
		$product = new Product_Domain();
		
		$product->setProductId($productId);
		$product->setProductName($productName);
		$product->setProductPrice($productPrice);
		$product->setProductDescription($productDescription);
		$product->setArchive($productArchive);
		$product->setCategory($productCategoryId);
		$product->setImgDirectory($productImg);
		
		$productService = new ProductService();
		$result["rowsAffected"] = $productService->updateProductInfo($product);
		echo json_encode($result);
	}
	
	public function addProduct(){
		
		$productName = $this->post('productName');
		$productPrice = $this->post('productPrice');
		$productDescription = $this->post('description');
		$productCount = $this->post('count');
		$productCategoryId = $this->post('category');
		$productImg = $this->post('img');
		
		$product = new Product_Domain();
		
		$product->setProductName($productName);
		$product->setProductPrice($productPrice);
		$product->setProductDescription($productDescription);
		$product->setProductQty($productCount);
		$product->setCategory($productCategoryId);
		$product->setImgDirectory($productImg);
		
		$productService = new ProductService();
		$result["rowsAffected"] = $productService->addProduct($product);
		echo json_encode($result);
	}
	
	public function unarchive(){
		
		$productId = $this->post('productId');
		
		$product = new Product_Domain();
		$product->setProductId($productId);
		
		$productService = new ProductService();
		$result["rowsAffected"] = $productService->unarchive($product);
		echo json_encode($result);
	}
	
	public function getAvailableImages(){
		$productService = new ProductService();
		$this->view->setDisplay('avaiImages', $productService->getAvailableImages());
		$this->view->template('components/availableImages');
	}
}